<template>
  <div class="contract_manage_list">
    <div class="model-title-css">
      <div>人员档案</div>
      <!-- <el-button class="liuchengbtn" style="margin-left:10px;" type="text" @click="processDialog=true">
        <i class="iconfont "></i> 流程示意图
      </el-button> -->
    </div>
    <!-- <el-dialog width="1000px" class="dialog" title="流程示意图" :visible.sync="processDialog" v-dialog-drag :modal-append-to-body="false" :close-on-click-modal="false">
      <el-image style="display:block;height:600px" class="g-dialog-img" :src="require('../../../assets/images/liuchengtu/personArchive.png')" alt="" :preview-src-list="[require('../../../assets/images/liuchengtu/personArchive.png')]" />
    </el-dialog> -->

    <div class="background_wrap public-flex-row">
      <div class="left-box WholeScroll">
        <el-tree class="Public-Tree-Class" :data="leftList" :props="defaultProps" ref='treeData' :check-on-click-node="true" node-key="num" :highlight-current='true' :default-expanded-keys="defaultTreeKey" @node-click="handleNodeClick"></el-tree>
      </div>
      <div class="right-box">
        <el-tabs class="approve-tabs" v-model="activeName" v-if="chooseData.personType == '1'">
          <el-tab-pane label="进出场登记" name="first"></el-tab-pane>
          <!-- <el-tab-pane label="人员变更" name="second"></el-tab-pane> -->
        </el-tabs>
        <inOutSign v-if="activeName == 'first'" :chooseData="chooseData"></inOutSign>
        <peopleChange v-if="activeName == 'second'" :chooseData="chooseData" :routerData="routerData"></peopleChange>
      </div>
    </div>

  </div>
</template>

<script>
import {
  tableRowStyleGol,
  tableCellStyleGol,
  tableHeaderCellStyleGol,
  doCreateObject
} from '@/utils/publicFun'

import msgUtil from '@/utils/msgUtil'
import projectSectionInfo from '@/api/buss/common'
import measureother from '@/api/buss/measureother'
import crudDict from '@/api/system/dict'
import personChange from '@/api/buss/personChange'

import publicSearchData from '@/mixins/publicSearchData'
export default {
  mixins: [publicSearchData],
  name: `demo`,
  components: {
    inOutSign: require(`./in-out-sign/in-out-sign.vue`).default,
    peopleChange: require(`./people-change/people-change.vue`).default,
  },
  data() {
    return {
      processDialog: false,
      chooseData: {},
      nowChooseData: {},
      activeName: 'first',
      leftList: [],
      defaultTreeKey: [],
      defaultProps: {
        children: 'children',
        label: 'name',
      },
      permission: {
        add: ['100', '1000077:add'],
        show: ['100', '1000077:getOne'],
        edit: ['100', '1000077:edit'],
        del: ['100', '1000077:del'],
      },

      tableRowStyleName: tableRowStyleGol,
      tableCellStyleName: tableCellStyleGol,
      tableHeaderCellStyleName: tableHeaderCellStyleGol,
      tablePaginationPrev: '上一页',
      tablePaginationNext: '下一页',
      hasAdd: false, //是否有新增权限
      loading: false,
      //  当前查看的文件列表
      nowFileList: [],
      showFileDialog: false,
      list: [{}, {}, {}, {}],
      selectArr: [],
      fileTypeArr: [{
        name: '交工证书',
        id: '1'
      }, {
        name: '7天强度报告',
        id: '2'
      }, {
        name: '28天强度报告',
        id: '3'
      }, {
        name: '监理收验资料',
        id: '4'
      }, {
        name: '桩检报告',
        id: '5'
      }, {
        name: '质检资料',
        id: '6'
      }, {
        name: '评定资料',
        id: '7'
      }, {
        name: '检验申请批复单',
        id: '8'
      }, {
        name: '其他证书',
        id: '9'
      }],
      chooseJlValue: '',
      title: '人员',
      // 搜索参数
      search: {
        phaseId: '',
        pageNo: 0,
        pageSize: 10,
        total: 0,
        sectionId: '',
        projectId: '',
        type: '',
      },

      // 向详情子组件的传值
      postDetailData: {
        show: false,
        type: 'build',
        typeName: '新增',
      },
      // 向详情子组件的传值
      treeEditDialogData: {
        show: false,
        nowTreeData: []
      },
      allowSelContract: false,
      payTypeArr: [],
      payTypeArr2: [],
      routerData: null,
    }
  },
  mounted() {
    if (this.$route.query != undefined && this.$route.query.id != null) {
      console.log(this.$route.query)
      console.log(this.$route.params)
      this.routerData = this.$route.query

      this.getData(this.routerData.id)
    }
    this.initData()
  },
  computed: {
    tableHeight: function() {
      return (window.innerHeight - 300) + 'px';
    },
    changeHeaderProject() {
      return this.$store.state.settings.changeHeaderProject
    }
  },
  watch: {
    changeHeaderProject: {
      immediate: false,
      handler: function(val) {
        if (!this.$headerSearch) {
          return
        }
        this.$nextTick(() => {
          if (val != '') {
            this.defaultTreeKey = []
            this.leftList.forEach((item, index) => {
              if (item.id == val) {
                console.log(item, 'item')
                if (item.children[0].hasOwnProperty('children') && item.children[0].children.length > 0) {
                  this.defaultTreeKey = [item.children[0].num]
                  this.$refs['treeData'].setCurrentKey(item.children[0].children[0].num);
                  this.handleNodeClick(item.children[0].children[0])
                } else {
                  this.defaultTreeKey = [item.num]
                  this.$refs['treeData'].setCurrentKey(item.num);
                  this.handleNodeClick(item)
                }
              }
            })
          } else {
            let lastLeaf = null
            lastLeaf = this.$publicJs.getDeepSon(this.leftList, 'children')
            this.defaultTreeKey = []
            this.defaultTreeKey.push(lastLeaf.num)
            this.$refs['treeData'].setCurrentKey(lastLeaf.num);
            this.$set(this, 'chooseData', lastLeaf)
          }
        })
      }
    }
  },
  methods: {
    showDetailDialog(flag, params) {
      console.log(flag, params)
      if (!params) {
        params = {}
      }
      switch (flag) {
        case 'build':
          params.sectionId = this.search.sectionId
          this.postDetailData = {
            show: true,
            type: 'build',
            typeName: '新增',
            params: params,
          }
          break
      }
    },
    getDicts() {
      //获取预付款类型
      crudDict.getByCode('prepaymentType').then(res => {
        if (res.code === 200) {
          this.payTypeArr = res.data
        } else {
          msgUtil.error2(res.data.message)
        }
      })
      //获取扣回预付款类型
      crudDict.getByCode('deducePrepayment').then(res => {
        if (res.code === 200) {
          this.payTypeArr2 = res.data
        } else {
          msgUtil.error2(res.data.message)
        }
      })
    },
    addTableRow(row) {
      row.phaseId = this.search.phaseId
      this.postDetailData = {
        show: true,
        type: 'build',
        typeName: '新增',
        rowData: row
      }
    },
    initData() {
      this.$httpPost(`buss/entryexitregistration/getLeftTree`).then(res => {
        if (res.code === 200) {
          this.$set(this, 'leftList', res.data)
          this.$nextTick(() => {
            if (!this.routerData) {
              if (this.$headerSearch) {
                if (this.changeHeaderProject) {
                  let val = this.changeHeaderProject
                  this.leftList.forEach((item, index) => {
                    if (item.id == val) {
                      console.log(item, 'item')
                      if (item.children[0].hasOwnProperty('children') && item.children[0].children.length > 0) {
                        this.defaultTreeKey = [item.children[0].num]
                        this.$refs['treeData'].setCurrentKey(item.children[0].children[0].num);
                        this.handleNodeClick(item.children[0].children[0])
                      } else {
                        this.defaultTreeKey = [item.num]
                        this.$refs['treeData'].setCurrentKey(item.num);
                        this.handleNodeClick(item)
                      }
                    }
                  })
                } else {
                  let lastLeaf = null
                  lastLeaf = this.$publicJs.getDeepSon(this.leftList, 'children')
                  this.defaultTreeKey = []
                  this.defaultTreeKey.push(lastLeaf.num)
                  this.$refs['treeData'].setCurrentKey(lastLeaf.num);
                  this.$set(this, 'chooseData', lastLeaf)
                }
              } else {
                //如果没有路由传参+ 关掉了头部筛选
                let lastLeaf = null
                lastLeaf = this.$publicJs.getDeepSon(this.leftList, 'children')
                this.defaultTreeKey = []
                this.defaultTreeKey.push(lastLeaf.num)
                this.$refs['treeData'].setCurrentKey(lastLeaf.num);
                this.$set(this, 'chooseData', lastLeaf)
              }
            } else {

              this.defaultTreeKey = []
              this.defaultTreeKey.push(this.routerData.num)
              this.$refs['treeData'].setCurrentKey(this.routerData.num);
            }

          })
        } else {
          this.$messageError(res.data.message)
        }
      }).catch(() => {
        this.$messageError('获取项目标段树失败')
      })
    },
    getData(id) {
      personChange.getOne(id).then(res => {
        console.log(res, 'vvvvvvvvvvv')
        //代办跳转 赋值
        this.defaultTreeKey = []
        this.chooseData.personType = 1
        this.$nextTick(() => {
          this.$set(this, 'routerData', res.data)
        })
        this.defaultTreeKey.push(this.routerData.num)
        this.$refs['treeData'].setCurrentKey(this.routerData.num);
        this.activeName = 'second'
      }).catch(res => {
        console.log(res)
      })
    },
    deleteList() {
      let selectionArr = this.$refs['multipleTable'].selection
      if (selectionArr.length > 0) {
        this.$confirm('确定是否删除？', '提示', {
          confirmButtonClass: 'iconfont icon-icon_ok',
          cancelButtonClass: 'iconfont icon-icon_cancel',
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'info',
        })
          .then(() => {
            this.sealDealHttp(
              `/buss/imageProgressBreak/delete`, ``,
              { ids: selectionArr.map(item => item.id) },
              () => {
                this.$messageSuccess('删除成功!')
              })
          })
      } else {
        this.$messageError('请先选择要删除的记录！')
      }
    },

    // 事件处理的api请求
    sealDealHttp(url, query, param, callback) {
      this.$httpPost(url + query, param)
        .then((res) => {
          if (res.status === 1) {
            if (callback) callback(res)
            // 重新获取数据
            setTimeout(() => {
              this.getList(true)
            }, 1000)
          } else {
            this.$messageError(res.message)
          }
        })
        .catch((res) => {
          console.log('请求出现错误', res)
          console.log(res)
        })
    },
    handleNodeClick(data) {
      console.log("nowChooseData=", data)
      this.nowChooseData = data
      if (data.type == 'contract' || data.type == 'section') {
        this.chooseData = data
        this.activeName = 'first'
      }
    },
    changePhase() {
      this.getList()
    },
    // 格式化表单数据
    formatter(row, column) {
      let array = column.property.split('.')
      let result
      array.forEach((item, i) => {
        result = result === undefined ? row[item] : result[item]
      })
      if (result === '' || result === undefined) {
        return '--'
      } else {
        return result
      }
    },
    // 获取列表 api
    getList() {
      this.list = []
      measureother.getData(this.search.phaseId).then(res => {
        if (res.code == 200) {

        } else {
          msgUtil.error2(res.data.message)
        }
      })
      return
    },
    // 切换列表
    paginationChange(index) {
      this.search.pageNo = index - 1
      this.getList()
    },   // 显示详情弹窗 不同状态
    // 列表的操作流程
    handleClickDeal(res) {
      let type = res.type
      let detailId = res.detailId
      res.row.phaseId = res.row.measurePhaseId
      switch (type) {
        case 'build':
          this.postDetailData = {
            show: true,
            type: 'build',
            typeName: '新增',
            params: res,
          }
          break
        // 修改
        case 'update':
          // 显示详情弹窗
          this.postDetailData = {
            show: true,
            type: 'edit',
            typeName: '修改',
            rowData: res.row
          }
          break
        // 查看
        case 'show':
          // 显示详情弹窗
          this.postDetailData = {
            show: true,
            type: 'check',
            typeName: '查看',
            rowData: res.row
          }
          break
        // 删除
        case 'delete':
          this.$confirm('确定是否删除？', '提示', {
            confirmButtonClass: 'iconfont icon-icon_ok',
            cancelButtonClass: 'iconfont icon-icon_cancel',
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'info'
          }).then(() => {
            let idlist = []
            idlist.push(detailId)
            let param = {
              ids: idlist
            }
            measureother.del(param).then(() => {
              msgUtil.success2('删除成功!')
              this.getList()
            }).catch(() => {
              console.log('请求出现错误')
            })
          }).catch(() => {
            console.log('请求出现错误')
          })
          break
      }
    }
  },
}
</script>

<style scoped lang="scss">
.model-title-css {
  display: flex;
  flex-direction: row;
  align-items: center;
}
.list_wrap {
  margin-top: 20px;
}
.left-box {
  min-width: 220px !important;
  max-width: 350px !important;
  max-height: calc(100vh - 200px);
  border-right: 1px #e2e8ec solid;
  overflow: auto;
  p {
    cursor: pointer;
    font-size: 14px;
  }
}
.right-box {
  width: calc(100% - 200px);
  padding-left: 15px;
  flex-grow: 1;
  padding-right: 10px;
}
</style>
